<template>
	<view class="isapp">

		<view class="context">
			
			<u-modal :show="show2" :title="title"  showCancelButton="true" @confirm="confirm" @cancel="cancel" >
				<u--form
								labelPosition="left"
								:model="model1"
								:rules="rules"
								ref="form1"
						>
							<u-form-item
									label="理由"
									prop="userInfo.msg"
									borderBottom
									ref="item1"
							>
								<u--input
										v-model="model1.userInfo.msg"
										border="none"
										placeholder="请假理由"
										
								></u--input>
							</u-form-item>
							
						</u--form>
			
			</u-modal>
			<template v-if="show">
				<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text="今天没有可预约课程">
				</u-empty>
			</template>

			<view class="makelist" v-if="!show">
				<view class="item" v-for="(item,index) in makelist">
					<view class="image">
						<u-image :showLoading="true" :src="item.image" width="80px" height="80px" @click="click">
						</u-image>
					</view>
					<view class="text">
						<view class="text1">{{item.about_time}}{{item.project_name}}</view>
						<view class="text2">
							<view>{{item.site}}</view>
							<template v-if="type==1">
								
							
							<view class="qj" v-if="false || item.leave_id">
								<u-tag text="以发起请假"  plain size="mini"></u-tag>
							</view>
							<view class="qj" v-else>
								<u-tag text="请假" @click="leav(item.application_id,item.classify)" plain size="mini"></u-tag>
							</view>
							</template>
							
						</view>
						<view class="text3"><text class="u-line-1">{{item.target}}</text></view>
						<view class="">
							{{dateFormat(item.that_very_day)}}
						</view>
					</view>
				</view>

			</view>

		</view>
		<u-loadmore :status="status" color="#fff" />
	</view>
</template>

<script>
	import {
		getisappliAll,
		getleave
	} from "@/api/index.js"
	export default {
		data() {
			return {
				makelist: [],
				pageNum: 1,
				hasNextPage: 5,
				status: 'loadmore',
				show: false,
				show1: false,
				show2:false,
				title: '请假后请联系管理员',
				model1: {
								userInfo: {
									msg: ''
									
								},
							},
								rules: {
											'userInfo.msg': {
												type: 'string',
												required: true,
												message: '请填写请假理由',
												trigger: ['blur', 'change']
											},
											
			},
			id:'',
			classify:'',
			type:''
			}
		},
		onLoad(e) {
			let app = getApp();
			 this.userLoginFlag = app.globalData.userLogin;
				console.log(app.globalData.userLogin);
			this.type = e.type
			this.getisappliAlls()
		},
		onReady() {
				//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		    	this.$refs.form1.setRules(this.rules)
		    },
		methods: {
			async getisappliAlls() {
				let openid = uni.getStorageSync("openid")
				const {
					data,
					status,
					msg
				} = await getisappliAll({
					params: {
						openid: openid,
						pageNum: this.pageNum,
						pageSize: 6,
					}
				})

				if (status == 200) {
					this.makelist = data.list
					this.status = 'loadmore'
					this.hasNextPage = data.size
				}else{
					
					uni.showToast({
												title: "请登录开通会员",
												duration: 2000,
												icon:"error"
											
											})
				}

			},
			dateFormat(time) {
				let date = new Date(time);
				let year = date.getFullYear();
				// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
				let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
				let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
				let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
				let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				// 拼接
				return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
				// return year + "-" + month + "-" + day;
			},
			leav(id,classfiy){
				this.show2 = true
				this.id = id
				this.classify = classfiy
				
			},
			cancel(){
				
				this.show2=false
			},
			confirm(){
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.form1.validate().then(res => {
					
						this.show2=false
						this.getleaves()
						
					
					
				}).catch(errors => {
					uni.$u.toast('校验失败')
					return
				})
				
			},
			async getleaves(){
				let openid = uni.getStorageSync("openid")
				let userinfo = uni.getStorageSync("userinfo")
				console.log(openid);
				const {
					data,
					status,
					msg
				} = await getleave({
					applicationId:this.id,
					leaveName:userinfo.nickName,
					mobile:userinfo.mobile,
					classify:this.classify,
					leaveMsg:this.model1.userInfo.msg,
					openid:openid
				})
				if(status == 200){
					
					uni.showToast({
						title: '发起请假成功请联系管理员微信进行审核',
						duration: 2000,
						icon:"none"
					});
				}
			}
		},
		onReachBottom() {

			this.status = 'loading';
			this.pageNum = ++this.pageNum;

			setTimeout(() => {
				if (this.hasNextPage == 6) {
					this.getisappliAlls()

				}

				if (this.hasNextPage == 6) {
					this.status = 'loading'
				} else {
					this.status = 'nomore'
					this.hasNextPage = 1
				}
			}, 2000)
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #4f4a73;
		height: 100vh;
	}

	.isapp {
		background-color: #4f4a73;
		height: 100vh;
	}

	.context {
		background-color: #4f4a73;
		padding-bottom: 40rpx;

	}

	.makelist {
		background-color: #fff;
		width: 95%;
		margin: 40rpx auto;
		border-radius: 25rpx;
		box-sizing: border-box;
		padding: 25rpx;

		.item {
			display: flex;
			border-bottom: 1rpx solid #4f4a73;
			margin-bottom: 40rpx;
			height: 200rpx;

			&:last-child {
				border-bottom: none;
				margin-bottom: 0;
			}

			.text {
				margin-left: 40rpx;
				color: $u-content-color;

				.text1 {
					font-size: 32rpx;
					font-weight: bold;
					color: $u-main-color;
				}

				.text2 {
					display: flex;
					justify-content: space-between;
					.qj{
						margin-top: -20rpx;
					}
				}

				.text4 {
					border: 1rpx solid #dd1c34;
					text-align: center;
					width: 140rpx;
					border-radius: 25rpx;
					font-weight: 400;
					color: #dd1c34;
				}

			}
		}
	}
</style>
